<?php
namespace app\service;

class camService
{
	public function testmsg($car_number)
	{
		$ledline1="车位已满";
		$ledline2="禁止通行";
		$base64str=$this->getstr($car_number,$ledline1,$ledline2);
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	//车位显示开闸命令
	public function getopennummsg($voicetext,$ledline1,$ledline2)
	{
		$base64str=$this->getstr($voicetext,$ledline1,$ledline2);
		return   '{
				"Response_AlarmInfoPlate": {
					"info": "ok",
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	public function noopennummsg($voicetext,$ledline1,$ledline2)
	{
		$base64str=$this->getstr($voicetext,$ledline1,$ledline2);
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	//获取语音播报，显示屏显示的str
	public function getstr($voicetext,$ledline1,$ledline2)
	{
		//DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
	}
	
	public function gettemporarycommand($car_number){
		$base64str=$this->temporarymsg($car_number);
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	public function getspecialcommand($car_number){
		$base64str=$this->specialmsg($car_number);
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	public function getmulticarcommand($car_number){
		$base64str=$this->multicarmsg($car_number);
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	public function multicarmsg($car_number){
		$voicetext=$car_number.",禁止通行";
		$ledline1="车位已满";//显示屏第1行显示内容
        $ledline2="禁止通行";//显示屏第2行显示内容
		

        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
	}
	public function specialmsg($car_number){
		$voicetext=$car_number.",禁止通行";
		$ledline1="地库车";//显示屏第1行显示内容
        $ledline2="无权通行";//显示屏第2行显示内容
		

        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
	}
	public function temporarymsg($car_number){
        $voicetext=$car_number.",一路顺风";
        $ledline1="访客车辆";//显示屏第1行显示内容
        $ledline2="请登记入场";//显示屏第2行显示内容
		

        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
    }
	//地库车命令
	public function getlibrarycommand($car_number,$in_time,$out_time){
		$base64str=$this->librarymsg($car_number,$in_time,$out_time);
		return '{ "Response_AlarmInfoPlate": 
					{
						"info": "ok",
						"serialData": [
							{
								"serialChannel": 0,
								"data": "'.$base64str.'",
								"dataLen": 123
							}
						],
						"white_list_operate":{
							"operate_type" : 0,
							"white_list_data": 
								[ 
									{ 
										"plate": "'.$car_number.'",
										"enable": 1,
										"need_alarm": 0, 
										"enable_time": "'.date('Y-m-d H:i:s',$in_time).'", 
										"overdue_time": "'.date('Y-m-d H:i:s',$out_time).'" 
									}
								]
						} 
					} 
				}';
	}
	//地库车信息
	public function librarymsg($car_number,$in_time,$out_time){
		$ctime=date("Y年m月d日",$out_time);
		$voicetext=$car_number.",月租车,有效期至".$ctime;
		//$voicetext="月租车,有效期至".$ctime;
        $ledline1=$car_number;//显示屏第1行显示内容
		$ledline2="地库车";//显示屏第2行显示内容
        //$ledline2="有效期至".$ctime;//显示屏第2行显示内容
		
		 /*$text:led显示屏内容
		 *  $LID:led显示行号
		 *  $DM :led显示模式
		 *  $DS :为显示速度
		 *  $DT :停留时间
		 *  $DR ：为显示次数
		 *  $TC ：为文本颜色*/
        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","00","01","10","05","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","05","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
	}
	public function getpackagecommand($car_number,$in_time,$out_time){
		$base64str=$this->packagemsg($car_number,$in_time,$out_time);
		return '{ "Response_AlarmInfoPlate": 
					{
						"info": "ok",
						"serialData": [
							{
								"serialChannel": 0,
								"data": "'.$base64str.'",
								"dataLen": 123
							}
						],
						"white_list_operate":{
							"operate_type" : 0,
							"white_list_data": 
								[ 
									{ 
										"plate": "'.$car_number.'",
										"enable": 1,
										"need_alarm": 0, 
										"enable_time": "'.date('Y-m-d H:i:s',$in_time).'", 
										"overdue_time": "'.date('Y-m-d H:i:s',$out_time).'" 
									}
								]
						} 
					} 
				}';
	}
	public function packagemsg($car_number,$in_time,$out_time){
		$ctime=date("Y年m月d日",$out_time);
		$voicetext=$car_number.",月租车,有效期至".$ctime;
		//$voicetext="月租车,有效期至".$ctime;
        $ledline1=$car_number;//显示屏第1行显示内容
		$ledline2="月租车";//显示屏第2行显示内容
        //$ledline2="有效期至".$ctime;//显示屏第2行显示内容
		
		 /*$text:led显示屏内容
		 *  $LID:led显示行号
		 *  $DM :led显示模式
		 *  $DS :为显示速度
		 *  $DT :停留时间
		 *  $DR ：为显示次数
		 *  $TC ：为文本颜色*/
        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","00","01","10","05","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","05","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
	}
	public function getopencommand($base64str){
		return '{
				"Response_AlarmInfoPlate": {
					"info": "ok",
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	public function getvoicecommand($base64str){
		return   '{
				"Response_AlarmInfoPlate": {
					"serialData": [
						{
							"serialChannel": 0,
							"data": "'.$base64str.'",
							"dataLen": 123
						}
					]
				}
			}';
	}
	
    public function comemsg($car_number){
        $str=$car_number.",欢迎光临,请入场停车";

    }
    //付款成功,语音播报及车牌显示
    public function outmsg1($car_number){
        $voicetext=$car_number.",一路顺风";
        $ledline1=$car_number;//显示屏第1行显示内容
        $ledline2="一路顺风";//显示屏第2行显示内容
		

        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
    }
    //出场未付款语音播报及车牌金额显示
    public function outmsg2($car_number,$money){
        $voicetext=$car_number.",请缴费".$money."元";
        $ledline1=$car_number;//显示屏第1行显示内容
        $ledline2=$money."元";//显示屏第2行显示内容
		

        //设置显示屏显示内容及语音播报内容
        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部
        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
    }

    public function comevoice($car_number){
        $voicetext=$car_number.",欢迎光临,请入场停车";//语音播报内容
        $ledline1=$car_number;//显示屏第一行显示内容
        $ledline2="欢迎光临,请入场停车";

        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部

        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
        $led1=$this->ledString2Hex($ledline1,"00","03","00","08","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","08","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
		$str=$this->base64str($str);
        return $str;
    }
	public function comevoice1($car_number){
        $voicetext=$car_number.",欢迎光临,请入场停车";//语音播报内容
        $ledline1=$car_number;//显示屏第一行显示内容
        $ledline2="欢迎光临,请入场停车";

        //DA + VR + PN[2] + 0x6E
        $head="00 64 FF FF 6E";//语音播报加显示屏显示头部

        $SAVE_FLAG="00";//存贮到临时区
        $TEXT_CONTEXT_NUMBER="02";//led显示屏显示两行
		
        $led1=$this->ledString2Hex($ledline1,"00","03","00","10","01","FF 00 00 00");
        $led2=$this->ledString2Hex($ledline2,"01","01","01","10","01","00 FF 00 00");
        $voice=$this->voiceString2Hex($voicetext);
        $text_con=$SAVE_FLAG." ".$TEXT_CONTEXT_NUMBER." ".$led1." 0D ".$led2." 00 ".$voice;
        $DL=$this->countbyte($text_con);
        $str=$head." ".$DL." ".$text_con;
        //对数据进行crc16校验并获得字符串
        $str=$this->getcrc16($str);//crc16校验
		$str=$this->base64str($str);
        return $str;
    }


    public function comeviocetest($car_number){
        $text=$car_number.",欢迎光临,请入场停车";
        $DL=dechex(strlen(iconv("utf-8","gb2312//IGNORE",$text))+1);
        //return $DL
        $text=$this->String2Hex($text);//文本的16进制编码
        $str="00 64 FF FF 30 ".$DL." 01 ".$text;
        $str=$this->getcrc16($str);//crc16校验
        $str=$this->base64str($str);
        return $str;
    }

    //统计字符串有多少字节
    protected function countbyte($str){
        $num=count(explode(" ",$str));
        $len=dechex($num);
        if(strlen($len)=="1"){
            $len="0".$len;
        }
        return $len;
    }

    //led显示屏转换为2进制
    /**
     *  $text:led显示屏内容
     *  $LID:led显示行号
     *  $DM :led显示模式
     *  $DS :为显示速度
     *  $DT :停留时间
     *  $DR ：为显示次数
     *  $TC ：为文本颜色
     */
    public function ledString2Hex($text,$LID,$DM,$DS,$DT,$DR,$TC){
        $str=$LID." ".$DM." ".$DS." ".$DT." ".$DR." ".$TC;
        $TL=dechex(strlen(iconv("utf-8","gb2312//IGNORE",$text)));
        if(strlen($TL)=="1"){
            $TL="0".$TL;
        }
        $ledcon=$this->String2Hex($text);
        $str=$str." ".$TL." ".$ledcon;
        return $str;
    }

    //语音播报转换为2进制
    protected function voiceString2Hex($str){
        $VF="0A";//语音开始标志
        //语音文本长度
        $TL=dechex(strlen(iconv("utf-8","gb2312//IGNORE",$str)));
        if(strlen($TL)=="1"){
            $TL="0".$TL;
        }
        //语音内容转换为2进制
        $con=$this->String2Hex($str);
        $result=$VF." ".$TL." ".$con." "."00";//00语音结束标志符
        return $result;
    }

    //字符串转16进制
    public function String2Hex($string){
        $string=iconv("utf-8","gb2312//IGNORE",$string);
        $hex='';
        $t=0;
        for ($i=0; $i < strlen($string); $i++){
            if($t=="0"){
                $hex=$hex.dechex(ord($string[$i]));
            }else{
                $hex=$hex." ".dechex(ord($string[$i]));
            }
            $t=$t+1;
        }
        return $hex;
    }
    //16进制字符串base64编码
    public function base64str($str){
        $str = explode(" ",$str);

        $this->everyto8($str);

        $endmod=$this->checktotal($str);
        $str=$this->divide6($str);
        $this->addzero($str);
        $this->everytodec($str);
        $base64code=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','=');
        $result =$this->encode64($str,$base64code,$endmod);
        return $result;
    }
//-----------16进制base64编码封装方法---------------------------------
    private function to8($str){
        if(strlen($str)<8){
            for($i=8-strlen($str);$i>0;$i--){
                $str="0".$str;
            }
        }
        return $str;
    }
    private function everyto8(&$arr){
        for($i=0;$i<count($arr);$i++){
            $arr[$i]=$this->to8(base_convert($arr[$i], 16, 2));
        }
    }
    private function everytodec(&$arr){
        for($i=0;$i<count($arr);$i++){
            $arr[$i]=base_convert($arr[$i], 2, 10);
        }
    }
    private function checktotal(&$arr){
        $endm=3-count($arr)%3;
        if(count($arr)%3 !=0){
            for($i=0;$i<$endm;$i++){
                $arr[]="00000000";
            }
            return $endm;
        }else{
            return 0;
        }
    }
    private function divide6($arr){
        $str="";
        foreach($arr as $val){
            $str.=$val;
        }
        if(strlen($str)%6 !=0)die("error");
        for($i=0;$i<strlen($str)/6;$i++){
            $result[]=substr($str,$i*6,6);
        }
        return $result;
    }
    function addzero(&$arr){
        for($i=0;$i<count($arr);$i++){
            $arr[$i]=$this->to8($arr[$i]);
        }
    }
    private function encode64($arr,$table,$endmod2){
        $result="";
        for($i=0;$i<count($arr)-$endmod2;$i++){
            $result.=$table[$arr[$i]];
        }
        for($i=0;$i<$endmod2;$i++){
            $result.="=";
        }
        return $result;
    }
//-----------------------------------------------------------
    //crc16校验后的16进制字符串
    protected function getcrc16($str){
        $arr=explode(" ",$str);
        $string=implode('',$arr);
        $s = pack('H*', $string);
        $t = $this->crc16($s);
        $low=dechex($t%256);//低字节
        if(strlen($low)=="1"){
            $low="0".$low;
        }
        $high=dechex(floor($t/256));//高字节
        if(strlen($high)=="1"){
            $high="0".$high;
        }
        $re=$str." ".$low." ".$high;
        return $re;
    }
    //crc16校验
    protected function crc16($string) {
        $crc = 0xFFFF;
        for ($x = 0; $x < strlen ($string); $x++) {
            $crc = $crc ^ ord($string[$x]);
            for ($y = 0; $y < 8; $y++) {
                if (($crc & 0x0001) == 0x0001) {
                    $crc = (($crc >> 1) ^ 0xA001);
                } else { $crc = $crc >> 1; }
            }
        }
        return $crc;
    }
}